package misc

import (
	"errors"
	"github.com/gin-gonic/gin"
	"strings"
)

type RequestInfo struct {
	Api     string
	Host    string
	Version string
	Path    string
}

func GetRequestInfo(path string, ctx *gin.Context) (rtn RequestInfo, err error) {

	//path := ctx.Request.URL.Path
	//
	//uris := strings.Split(path, context+"/")

	//if len(uris) <= 1 {
	//	err = errors.New(path + " is not good")
	//	return
	//}
	if len(path) <= 1 {
		err = errors.New(path + " is not good")
		return
	}

	host := ctx.Request.Header.Get("endpoint")

	if len(host) == 0 {
		//err = errors.New("not set endpoint in request header, you must set endpoint in your request header")
		//return
		host = "unix:/var/run/docker.sock"
	}

	version := ctx.Request.Header.Get("endpoint_version")

	if len(version) == 0 {
		version = "v1.46"
	}

	if strings.Index(version, "v1.") != 0 {
		err = errors.New("endpoint_version is not right, please check it in request header")
		return
	}

	rtn.Host = host
	//rtn.Api = uris[1]
	rtn.Api = path
	rtn.Version = version
	//rtn.Path = "/" + version + "/" + uris[1]
	//rtn.Path = "/" + version + "/" + path
	rtn.Path = "/" + version + path

	return
}
